package com.yan.utils;

import com.yan.anno.MyJsonFormat;
import com.yan.anno.MyJsonIgnore;
import com.yan.anno.MyJsonProperty;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * @author taoge
 * @Date 2024/8/15 11:47
 * @Description
 */
public final class JsonUtil {
    public JsonUtil() {
    }

    // 注解版对象转json格式字符串
    public static String toJsonString(Object obj){
        // {"author":"yft","createDate":1723719124337,"name":"三国演义","price":19.9}
        if (obj == null){
            return "";
        }
        try {
            Class<?> c = obj.getClass();
            StringBuffer sb = new StringBuffer();
            sb.append("{");
            Field[] declaredFields = c.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                if(declaredField.isAnnotationPresent(MyJsonIgnore.class)){
                    continue;
                }
                if(declaredField.isAnnotationPresent(MyJsonProperty.class)){
                    String fieldKey = declaredField.getAnnotation(MyJsonProperty.class).value();
                    fieldKey = "\"" + fieldKey + "\"";
                    String fieldValue = (String) declaredField.get(obj);
                    fieldValue = "\"" + fieldValue + "\"";
                    sb.append(fieldKey + ":" + fieldValue + ",");
                    continue;
                }
                if(declaredField.isAnnotationPresent(MyJsonFormat.class)){
                    String pattern = declaredField.getAnnotation(MyJsonFormat.class).pattern();
                    String fieldKey = declaredField.getName();
                    fieldKey = "\"" + fieldKey + "\"";
                    Date fieldValue = (Date) declaredField.get(obj);
                    String newFieldValue = new SimpleDateFormat(pattern).format(fieldValue);
                    newFieldValue = "\"" + newFieldValue + "\"";
                    sb.append(fieldKey + ":" + newFieldValue + ",");
                    continue;
                }

                if(declaredField.getGenericType().getTypeName().contains("String")){
                    String fieldKey = declaredField.getName();
                    fieldKey = "\"" + fieldKey + "\"";
                    String fieldValue = (String) declaredField.get(obj);
                    fieldValue = "\"" + fieldValue + "\"";
                    sb.append(fieldKey + ":" + fieldValue + ",");
                    continue;
                }
                if(declaredField.getGenericType().getTypeName().contains("Double")){
                    String fieldKey = declaredField.getName();
                    fieldKey = "\"" + fieldKey + "\"";
                    Double fieldValue = (Double) declaredField.get(obj);
                    sb.append(fieldKey + ":" + fieldValue + ",");
                }
                if(declaredField.getGenericType().getTypeName().contains("Integer")){
                    String fieldKey = declaredField.getName();
                    fieldKey = "\"" + fieldKey + "\"";
                    Integer fieldValue = (Integer) declaredField.get(obj);
                    sb.append(fieldKey + ":" + fieldValue + ",");
                }
            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append("}");
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // 转集合
    public static String toJsonArrayString(Object obj){
        // s = [{"author":"yft1","createDate":1723721470966,"name":"三国演义1","price":29.9},{"author":"yft2","createDate":1723721470966,"name":"三国演义2","price":39.9},{"author":"yft3","createDate":1723721470966,"name":"三国演义3","price":49.9}]
        try {
            if(obj == null){
                return "[]";
            }
            if(obj instanceof Map){
                throw new IllegalArgumentException("不能是集合");
            }
            StringBuffer sb = new StringBuffer();
            sb.append("[");
            Collection coll = (Collection) obj;
            for (Object o : coll) {
                String keyValue = toJsonString(o);
                sb.append(keyValue + ",");
            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append("]");
            return sb.toString();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 综合版
    public static String toJsonObjectString(Object obj){
        if(obj == null){
            return "[]";
        }
        if(obj instanceof Map){
            throw new IllegalArgumentException("不能是集合");
        }

        if(obj instanceof Collection){
            return toJsonArrayString(obj);
        }else{
            return toJsonString(obj);
        }
    }
}
